package part1.task3;

public class Consumer implements Runnable {
    private final Buffer<Object> buffer;
    private final String name;

    public Consumer(String name,Buffer<Object> buffer) {
        this.buffer = buffer;
        this.name=name;
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()){

            synchronized (buffer){
                if (buffer.isEmpty()){
                    try {
                        System.out.println("Consumer "+name+" wait.");
                        buffer.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("Consumer "+name+" get.");
                    buffer.get();
                    buffer.notifyAll();
                }
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
